package com.xbang.attendance.schedule;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.stock.Inventory;
import com.xbang.attendance.entity.stock.InventoryExchange;
import com.xbang.attendance.service.InventoryService;
import com.xbang.core.dao.search.component.SearchComponent;
import com.xbang.core.schedule.ScheduleProcess;
import com.xbang.core.utils.DateUtils;

public class ExchangeScheduleProcess implements ScheduleProcess {

	@Autowired
	private InventoryService inventoryService;

	@Override
	public void process() throws Exception {
		if (true) {
			return;
		}
		Map<Integer, BigDecimal> exchangeMap = getAllExchangeInfo();
		List<Inventory> inventorys = getAllInventory();
		for (Inventory inventory : inventorys) {
			if (exchangeMap.containsKey(inventory.getWarehouse().getId())) {
				inventory.setExchangeInventory(exchangeMap.get(inventory.getWarehouse().getId()));
				inventoryService.update(inventory);
			}
		}
	}

	/**
	 * 描述：获取库存信息
	 * 
	 * @return java.util.List
	 */
	private List<Inventory> getAllInventory() {
		List<Inventory> inventorys = new ArrayList<Inventory>();
		String hqlSentence = "FROM Inventory inventory WHERE 1 = 1 ";
		SearchComponent searchComponent = new SearchComponent();
		inventorys = inventoryService.searchForList(hqlSentence, searchComponent);

		return inventorys;
	}

	/**
	 * 描述:获取所有仓库调货信息
	 * 
	 * @return Map(Key:仓库ID ;Value:调动数量)
	 */
	private Map<Integer, BigDecimal> getAllExchangeInfo() {
		Map<Integer, BigDecimal> exchangeMap = new HashMap<Integer, BigDecimal>();

		List<InventoryExchange> inventoryExchanges = new ArrayList<InventoryExchange>();
		String hqlSentence = "FROM InventoryExchange inventoryexchange WHERE 1 = 1 ";

		Date tmpDate = DateUtils.addMonth(new Date(), -3);

		SearchComponent searchComponent = new SearchComponent();
		searchComponent.addSearchElement("geq.inventoryexchange.operationDate", tmpDate);

		inventoryExchanges = inventoryService.searchForList(hqlSentence, searchComponent);
		InventoryExchange inventoryExchange;
		for (int index = 0; index < inventoryExchanges.size(); index++) {
			inventoryExchange = inventoryExchanges.get(index);
			Integer sourceWarehouseId = inventoryExchange.getSourceWarehouse().getId();
			Integer indentWarehouseId = inventoryExchange.getIndentWarehouse().getId();

			if (exchangeMap.containsKey(sourceWarehouseId) && inventoryExchange.getAmount() != null) {
				exchangeMap.put(sourceWarehouseId, ((BigDecimal) exchangeMap.get(sourceWarehouseId)).subtract(inventoryExchange.getAmount()));
			} else {
				exchangeMap.put(sourceWarehouseId, new BigDecimal(0.00).subtract(inventoryExchange.getAmount()));
			}
			if (exchangeMap.containsKey(indentWarehouseId) && inventoryExchange.getAmount() != null) {
				exchangeMap.put(indentWarehouseId, ((BigDecimal) exchangeMap.get(indentWarehouseId)).add(inventoryExchange.getAmount()));
			} else {
				exchangeMap.put(indentWarehouseId, inventoryExchange.getAmount());
			}
		}

		return exchangeMap;
	}

	public InventoryService getInventoryService() {
		return inventoryService;
	}

	public void setInventoryService(InventoryService inventoryService) {
		this.inventoryService = inventoryService;
	}

}
